package io.gbznt.modules.buss.quartz;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.gbznt.common.utils.DateUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussInspectionTaskEntity;
import io.gbznt.modules.buss.service.BussInspectionTaskService;

public class MyJob implements Job {
	 @Autowired
	 private BussInspectionTaskService detailedDataService;
	
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    	JobKey key = jobExecutionContext.getJobDetail().getKey();
        JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        
		String name = dataMap.getString("name");
		String deptId = dataMap.getString("deptId");
		String handler = dataMap.getString("handler");
		/*String startTime = dataMap.getString("startTime");*/
		Date startTime = new Date();
		String bussFarmlandInfoIds = dataMap.getString("bussFarmlandInfoIds");
		String content = dataMap.getString("content");
		String createid = dataMap.getString("createId");
		BussInspectionTaskEntity btask = new BussInspectionTaskEntity();
		btask.setTaskName(name);
		btask.setDeptId(deptId);
		btask.setHandler(handler);
		
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/*try {
			//if(StringUtils.isNotBlank(startTime)){
				//Date date=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(startTime);
				btask.setStartTime(startTime);
			//}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		 btask.setStartTime(startTime);
		btask.setFarmlandInfoIds(bussFarmlandInfoIds);
		btask.setTaskContent(content);
		btask.setCreateId(createid);
		
		ValidatorUtils.validateEntity(btask,AddGroup.class);
	    
   	 
    	btask.setCreateTime(new Date());
    	//来源类型都是计划添加
    	btask.setSourceType(1);
    	//任务巡检编号用随机数代替
    	//btask.setBusinessNum((int)((Math.random()*9+1)*100000)+"");
    	// 生成工号
		String ymdStr = DateUtils.format(new Date(), "yyyyMMdd");// 年月日
		String businessNum = ymdStr + "X0001";
		BussInspectionTaskEntity lastBussMaintainTaskEntity = detailedDataService
				.getOne(new QueryWrapper<BussInspectionTaskEntity>().select("max(business_num) as business_num")
						.like("business_num", ymdStr));
		if (null != lastBussMaintainTaskEntity) {
			String lastNumStr = lastBussMaintainTaskEntity.getBusinessNum().substring(9);
			Integer lastNum = Integer.valueOf(lastNumStr) + 1;
			businessNum = ymdStr + "X" + String.format("%04d", lastNum);
		}
		btask.setBusinessNum(businessNum);
    	detailedDataService.save(btask);
		
    	//System.out.println("这是啥"+map);
    	
    	
        System.out.println(new Date() + ": doing something...");
    }
    
    public MyJob() {
        //new的时候注入需要的bean
        this.detailedDataService = ApplicationContextProvider.getBean(BussInspectionTaskService.class);
    }
    
}